package com.demo.consumer.controller.demoinfo;

import com.demo.consumer.config.JsonResult;
import com.demo.consumer.controller.apm.ApmDeomController;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("health")
public class healthController {

    private static final Logger log = LoggerFactory.getLogger(ApmDeomController.class);

    @Autowired
    private HttpServletRequest request;

    @Qualifier("userClient")
    @Autowired
    RestTemplate userRestTemplate;

    Integer count = 0;

    @GetMapping("failover")
    public void test() throws InterruptedException {
        System.out.println("get health");
        log.info("/failover sleep 1000ms");
        Thread.sleep(1000);
//        throw new RuntimeException("health 报个错");
    }

    @RequestMapping(value = "502")
    public ResponseEntity<Map<String,Object>> getUser() throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("server resp", "502");
        return new ResponseEntity<Map<String, Object>>(map, HttpStatus.BAD_GATEWAY);

    }

    @RequestMapping(value = "headers")
    public String headerinfo(){

        Map<String,String> map=new HashMap<String,String>();
        //1.获取所有请求头名称
        Enumeration<String> headerNames = request.getHeaderNames();
        //2.遍历
        while (headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            //根据名称获取请求头的值
            String value = request.getHeader(name);
            map.put(name,value);
        }
        JSONObject jsonObj = new JSONObject(map);
        String resultInfoOuterListStr = jsonObj.toString();

        System.out.println(request.getServerName()+":"+request.getServerPort());
        log.info("响应地址：{} {} {}",request.getServerName(), request.getServerPort(), resultInfoOuterListStr);
        return  "响应服务地址:" + request.getServerName()+":"+request.getServerPort()+ "\r\n"+resultInfoOuterListStr;

    }


    @GetMapping("getheaderinfo")
    public String getheaderinfo(){

        String headerurl = "http://127.0.0.1:8002/health/headers";
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("zhjcount",count.toString());
        count++;
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(requestHeaders);
        ResponseEntity<String> response = userRestTemplate.postForEntity(headerurl,httpEntity,String.class);
        return response.getBody();

    }




//    @GetMapping("/jsonmap")
//    public JsonResult<Map<String,Object>> getMap() throws IOException {
//
//        Map<String, Object> map = new HashMap<>();
//        map.put("CSDN地址", null);
//        map.put("粉丝数量", 4153);
//        return new JsonResult<>(map,HttpStatus.GATEWAY_TIMEOUT);
//
//    }

}
